package cn.uexpo.lhz.service.impl;

import cn.uexpo.lhz.service.AuthorizedWbService;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.request.AuthWeiboRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 小坏
 * @Date 2020/9/24 9:46
 * @Version 1.0
 * @program: 父工程 renren-cloud 万物起源之地
 */

@Service
public class AuthorizedWbServiceImpl implements AuthorizedWbService {

    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    public Object login(AuthCallback callback) {
        AuthRequest authRequest = getAuthRequest();
        AuthResponse login = authRequest.login(callback);
        AuthUser data = (AuthUser)login.getData();
        String msg = login.getMsg();
        int code = login.getCode();
        return data;
    }

    public Object revokeAuth(@PathVariable("token") String token) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.revoke(AuthToken.builder().accessToken(token).build());
    }

    public AuthRequest getAuthRequest() {
        return new AuthWeiboRequest(AuthConfig.builder()
                .clientId("55332797")
                .clientSecret("0354d044d32b258fab74a2422fe7c0d2")
                .redirectUri("http://8zjft2.natappfree.cc/wb/callback")
                .build());
    }
}
